package controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import serivce.House;
import serivce.LoginService;
import serivce.MapService;
import untity.FangZhi;
import untity.Position;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.*;

@Controller
@RequestMapping("/release")
public class Release {

    @Autowired
    private House house;
    @Autowired
    private MapService mapService;

    @Autowired
    private LoginService loginService;

    @RequestMapping("/page")
    public String releasePage() {

        return "release";
    }

    @RequestMapping(value = "/updata")
    @ResponseBody
    public JSONObject releaseGetData(HttpServletRequest request,@RequestBody JSONObject jsonObject) {
//        JSONObject jsonObject = JSON.parseObject(data).getJSONObject("data");
        System.out.println(jsonObject);
//        System.out.println(jsonObject);
//        System.out.println(data);
        System.out.println(request.getHeader("Content-Type"));
        JSONObject address = jsonObject.getJSONObject("form");
        System.out.println("address=" + address);
        String parkingSpance=jsonObject.getString("parkingSpace");
        String rentTime = jsonObject.getString("rentTime");
        String rentModel = jsonObject.getString("rentModel");
        String lookTime = jsonObject.getString("lookTime");
        String electric = jsonObject.getString("electric");
        String houseType = jsonObject.getString("houseType");
        JSONArray equipment = jsonObject.getJSONArray("equipment");
        String rentMode = jsonObject.getString("rentalType");
        String rent = jsonObject.getString("rent");
        String water = jsonObject.getString("water");
        String inTime = jsonObject.getString("inTime");
        String elevator = jsonObject.getString("elevator");
        String phone = jsonObject.getString("connection");
        String gas = jsonObject.getString("gas");
        String floor = jsonObject.getString("floor");
        String area = jsonObject.getString("area");
        FangZhi fangZhi = new FangZhi();
        if (rent != "") {
            fangZhi.setPrice(Integer.valueOf(rent));
        } else {
            fangZhi.setPrice(10000);
        }
        fangZhi.setHouseType(houseType);
        fangZhi.setGas(gas);
        fangZhi.setArea(area);
        fangZhi.setElectric(electric);
        fangZhi.setElevator(elevator);
        fangZhi.setFangShi(rentMode);
        if (floor != "") {
            fangZhi.setFloor(Integer.valueOf(floor));
        } else {
            fangZhi.setFloor(100);
        }

        fangZhi.setInTime(inTime);
        fangZhi.setWater(water);
        fangZhi.setRentMode(rentModel);
        fangZhi.setLookTime(lookTime);
        fangZhi.setLeaseTerm(rentTime);
        fangZhi.setPhone(phone);
        fangZhi.setChewei(parkingSpance);
        HashMap<String,Object> map=loginService.checkSession(request);

        String user_id= String.valueOf(map.get("id"));
        if (user_id=="null"){
            return JSON.parseObject("'id':'0'");
        }else{
            fangZhi.setUser_id(user_id);
        }
        house.addHouser(fangZhi);

        Position position = new Position();
        String province = address.getString("province");
        position.setProvince(province);
        String city = address.getString("city");
        position.setCity(city);
        String county = address.getString("area");
        position.setArea(county);

        position.setId(fangZhi.getId());
        System.out.println("这个是是房子id="+fangZhi.getId());
        String detailed = address.getString("detail");

        position.setDetail(detailed);
        String path = province + city + county + detailed;
        JSONObject code = mapService.getPosition(path);
        System.out.println(code);
        JSONObject result = code.getJSONObject("result");
        if (result != null) {
            System.out.println("111111");
            String level = result.getString("level");
            JSONObject location = result.getJSONObject("location");
            position.setLng(location.getString("lng"));
            position.setLat(location.getString("lat"));
            position.setLevel(level);
            house.setPosition(position);
        }
        String id = fangZhi.getId();
        if (equipment.size()!=0){
            List<String> list=new ArrayList<String>();
            for(int i=0;i<equipment.size();i++){
                list.add((String)equipment.get(i));
            }
            for (String a:list) {
                System.out.println("equipment="+a);
            }
            house.addEquipment(id,list);
        }

        System.out.println("id1=" + id);
        JSONObject json = JSON.parseObject("{'id':" + id + "}");

        return json;
//        return null;
    }

    @RequestMapping("/img")
    @ResponseBody
    public JSONObject getImg(HttpServletRequest request, @RequestParam("img") MultipartFile[] files, @RequestParam("id") String id) {
        OutputStream out = null;
        String[] list = new String[9];
        int num = 0;
        try {
            for (MultipartFile file : files) {
                System.out.println(file.getOriginalFilename());
                String path = request.getSession().getServletContext().getRealPath("/") + "html/imgs/" + file.getOriginalFilename();
                File f = new File(path);
                list[num] = "html/imgs/" + file.getOriginalFilename();
                num++;
                System.out.println(list[num-1]);
                out = new FileOutputStream(f);
                out.write(file.getBytes());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        house.putImg(id, list);

        return null;
    }

}

